package edu.ccut.saturn.studio.logic.model.saturn.logic;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.widgets.Display;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.logicdialog.SaturnLogicDialogInLogic;
import edu.ccut.saturn.studio.logic.LogicMultiPageEditor;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.ShapesDiagram;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnLogicNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnParameter;
import edu.ccut.saturn.studio.logic.model.saturn.action.SaturnLogicInAction;

public class SaturnLogicInLogic extends SaturnLogicNode {
	
	private static final long serialVersionUID = 7891463337712619843L;
 
	@SuppressWarnings("unchecked")
	public Dialog getDialog() {
		SaturnLogicDialogInLogic dialog = new SaturnLogicDialogInLogic(Display.getDefault().getShells()[0],this);
		super.reorder(this);
		return dialog;
	}

	public Shape cloneShape(Point mousePoint,Shape shape) {
		ShapesDiagram diagram = (ShapesDiagram)shape.getParent();
		if(diagram.getEditors() instanceof LogicMultiPageEditor){
			SaturnLogicInLogic saturnLogicNode = new SaturnLogicInLogic();
			SaturnLogicInLogic saturn = (SaturnLogicInLogic)shape;
			saturnLogicNode.setLocation(mousePoint);
			saturnLogicNode.setKey(saturn.getKey());
			saturnLogicNode.setName(saturn.getName());
			saturnLogicNode.setTransactionType(saturn.getTransactionType());
			saturnLogicNode.setException(saturn.getException());
			saturnLogicNode.setDescription(saturn.getDescription());
			saturnLogicNode.setParameters(saturn.getParameters());
			saturnLogicNode.setId(saturn.getId());
			saturnLogicNode.setNextId(saturn.getNextId());
			return saturnLogicNode;
		}else {
			SaturnLogicInAction saturnLogicNode = new SaturnLogicInAction();
			SaturnLogicInLogic saturn = (SaturnLogicInLogic)shape;
			saturnLogicNode.setLocation(mousePoint);
			saturnLogicNode.setKey(saturn.getKey());
			saturnLogicNode.setName(saturn.getName());
			saturnLogicNode.setTransactionType(saturn.getTransactionType());
			saturnLogicNode.setException(saturn.getException());
			saturnLogicNode.setDescription(saturn.getDescription());
			saturnLogicNode.setParameters(saturn.getParameters());
			saturnLogicNode.setId(saturn.getId());
			saturnLogicNode.setNextId(saturn.getNextId());
			return saturnLogicNode;
		}
	}
	public List<SaturnParameter> getListParameter(Document document) {
		
		List<SaturnParameter> paramaters = new ArrayList<SaturnParameter>();
		
		Node  root = document.getDocumentElement();
		Node StartNode = SaturnW3CUtil.getFirstChildByName(root, "start");
		Node ParamatersNode = SaturnW3CUtil.getFirstChildByName(StartNode, "parameters");
		List<Node> Paramaters = SaturnW3CUtil.getChildrenByName(ParamatersNode, "parameter");
		for (Node parameterNode: Paramaters) {
			SaturnParameter parameter = new SaturnParameter();
			parameter.setId(SaturnW3CUtil.getNodeAttributeValue(parameterNode, "id"));
			parameter.setName(SaturnW3CUtil.getNodeAttributeValue(parameterNode, "name"));
			parameter.setClassType(SaturnW3CUtil.getNodeAttributeValue(parameterNode, "classType"));
			parameter.setPassingType(SaturnW3CUtil.getNodeAttributeValue(parameterNode, "passingType"));
			parameter.setValueKey(SaturnW3CUtil.getChildNodeValue(parameterNode, "value-key"));
			parameter.setScope(SaturnW3CUtil.getChildNodeValue(parameterNode, "scope"));
			parameter.setDescription(SaturnW3CUtil.getChildNodeValue(parameterNode, "description"));
			paramaters.add(parameter);
			
		}	
		return paramaters;
	}
}
